package com.dbh.company.secret;


import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;

/**
 * @Description TODO
 * @Date 2020/6/30 11:43
 * @Auther wanghonggang
 */
public class KeyTest {


    public static final String KEY ="np!u5chin@adm!n1aaaaaaa2";

    private static final byte [] IV_BYTES = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

    public static void main(String [] args) throws Exception {
        String info =  "Z6EBiXuJ0zVZ4ACTYgViZw==";
        //System.out.println(encryptThreeDESSECB(info, true));
        System.out.println(decryptThreeDESCBC(info));
    }


    /**
     * 3DES-CBC加密
     */
    public static String encryptThreeDESCBC(String src) throws Exception {
        IvParameterSpec iv = new IvParameterSpec(IV_BYTES);

        DESedeKeySpec dks = new DESedeKeySpec(KEY.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
        SecretKey securekey = keyFactory.generateSecret(dks);

        Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, securekey, iv);
        byte[] b=cipher.doFinal(src.getBytes());

        return Base64.encode(b);
    }


    /**
     * 3DES-ECB加密
     * @param src
     * @param businessSwitchOnUseCBC
     * @return
     * @throws Exception
     */
    public static String encryptThreeDESSECB(String src, boolean businessSwitchOnUseCBC) throws Exception
    {
        if(StrUtil.isBlank(src)){
            return src;
        }
        String encryptStr = "";
        if(businessSwitchOnUseCBC){
            encryptStr = encryptThreeDESCBC(src);
        }else{
            DESedeKeySpec dks = new DESedeKeySpec(KEY.getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
            SecretKey securekey = keyFactory.generateSecret(dks);

            Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, securekey);
            byte[] b=cipher.doFinal(src.getBytes());
            encryptStr = Base64.encode(b);
        }

        return encryptStr;

    }


    public static String decryptThreeDESCBC(String src) throws Exception {
        IvParameterSpec iv = new IvParameterSpec(IV_BYTES);

        //--通过base64,将字符串转成byte数组
        byte[] bytesrc = Base64.decode(src.getBytes());
        //--解密的key
        DESedeKeySpec dks = new DESedeKeySpec(KEY.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
        SecretKey securekey = keyFactory.generateSecret(dks);

        //--Chipher对象解密
        Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, securekey, iv);
        byte[] retByte = cipher.doFinal(bytesrc);

        return new String(retByte);
    }

    /**
     * 3DES-ECB加密
     * @param src
     * @return
     * @throws Exception
     */
    public static String encryptThreeDESSECB(String src) throws Exception
    {
        String encryptStr = "";
        DESedeKeySpec dks = new DESedeKeySpec(KEY.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
        SecretKey securekey = keyFactory.generateSecret(dks);

        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, securekey);
        byte[] b=cipher.doFinal(src.getBytes());
        encryptStr = Base64.encode(b);

        return encryptStr;

    }


    public static String decryptThreeDESECB(String src) throws Exception {
        String decryptStr = "";
        try {

            //--通过base64,将字符串转成byte数组
            byte[] bytesrc = Base64.decode(src);
            //--解密的key
            DESedeKeySpec dks = new DESedeKeySpec(KEY.getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
            SecretKey securekey = keyFactory.generateSecret(dks);

            //--Chipher对象解密
            Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, securekey);
            byte[] retByte = cipher.doFinal(bytesrc);
            decryptStr = new String(retByte);
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return  decryptStr;
    }
}
